package com.ruoyi.system.controller;


import com.ruoyi.system.domain.Area;
import com.ruoyi.system.mapper.AreaMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/area")
public class AreaController {
    @Resource
    AreaMapper areaMapper;

    @GetMapping()
    //根据摄像头—模型关联的id获取区域，id为空获取所有区域
    public List<Area> findByCm_id(@RequestParam String cm_id){
        return areaMapper.findByCm_id(cm_id);
    }

    @DeleteMapping()
    //删除区域
    public String delete(@RequestParam String id){
        if(areaMapper.delete(id)){
            return "successful";
        }
        return "false";
    }

    @PostMapping
    //添加区域
    public String add(@RequestBody Area area){
        LocalDateTime Dt= LocalDateTime.now();
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyMMddHHmmss");
        String id = "area"+Dt.format(dtf);
        area.setId(id);
        if(areaMapper.add(area)){
            return "successful";
        }
        return "false";
    }

    @PutMapping
    //修改区域
    public String update(@RequestBody Area area){
        if(areaMapper.update(area)){
            return "successful";
        }
        return "false";
    }
}
